#!/bin/bash

echo "🚀 个人任务管理系统 - Docker启动脚本"
echo "========================================"
echo ""

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo "❌ Docker未安装！"
    echo "请参考 DOCKER-INSTALLATION.md 安装Docker Desktop"
    echo "下载地址: https://www.docker.com/products/docker-desktop/"
    exit 1
fi

# 检查Docker是否运行
if ! docker info &> /dev/null; then
    echo "❌ Docker未运行！"
    echo "请启动Docker Desktop应用"
    exit 1
fi

echo "✅ Docker已安装并运行"
echo ""

# 检查端口占用
echo "🔍 检查端口占用..."
if lsof -Pi :80 -sTCP:LISTEN -t >/dev/null 2>&1; then
    echo "⚠️  警告: 端口80已被占用"
    lsof -i :80
fi

if lsof -Pi :8080 -sTCP:LISTEN -t >/dev/null 2>&1; then
    echo "⚠️  警告: 端口8080已被占用"
    lsof -i :8080
fi

if lsof -Pi :3306 -sTCP:LISTEN -t >/dev/null 2>&1; then
    echo "⚠️  警告: 端口3306已被占用"
    lsof -i :3306
fi

echo ""
echo "🏗️  开始构建和启动服务..."
echo ""

# 启动服务
docker-compose up -d

echo ""
echo "⏳ 等待服务启动..."
sleep 10

echo ""
echo "📊 服务状态："
docker-compose ps

echo ""
echo "✅ 启动完成！"
echo ""
echo "📱 访问地址："
echo "   前端: http://localhost"
echo "   后端: http://localhost:8080/api"
echo "   MySQL: localhost:3306"
echo ""
echo "📝 查看日志："
echo "   docker-compose logs -f"
echo ""
echo "🛑 停止服务："
echo "   docker-compose down"
echo ""
